package com.idanchuang.component.logback.util;

import com.idanchuang.component.core.util.AbstractCryptUtil;
import com.idanchuang.component.logback.config.LogbackCryptConfig;
import com.idanchuang.component.logback.helper.LogContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 原始日志加解密工具类
 *
 * @author yjy
 */
@SuppressWarnings("all")
public class LogbackCryptUtil extends AbstractCryptUtil {

    private static final Logger log = LoggerFactory.getLogger(LogbackCryptUtil.class);

    public static String encrypt(String sSrc) {
        return encrypt(sSrc, LogbackCryptConfig.getInstance().orElse(new LogbackCryptConfig()));
    }

    public static String encrypt(String sSrc, LogbackCryptConfig cryptConfigBean) {
        try {
            return doEncrypt(sSrc, cryptConfigBean);
        } catch (Exception e) {
            LogContext.INTERNAL_LOGGER.info("logback encrypt str failed:[{}],rollback to source str:[{}]", e.getMessage(), sSrc);
        }
        return sSrc;
    }

    public static String decrypt(String sSrc) {
        return decrypt(sSrc, LogbackCryptConfig.getInstance().orElse(new LogbackCryptConfig()));
    }

    public static String decrypt(String sSrc, LogbackCryptConfig cryptConfigBean) {
        try {
            return doDecrypt(sSrc, cryptConfigBean);
        } catch (Exception e) {
            LogContext.INTERNAL_LOGGER.info("logback decrypt str failed:[{}],rollback to source str:[{}]", e.getMessage(), sSrc);
        }
        return sSrc;
    }

//    public static void main(String[] args) {
//        String a = "safsafkhsdkSDK返回司法考试到家好";
//        String b = encrypt(a);
//        String c = decrypt(b);
//        System.out.println(a);
//        System.out.println(b);
//        System.out.println(c);
//    }
}
